<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryParams.keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="changeValue"
        />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          background
          node-key="id"
          :data="data"
          :props="defaultProps"
          default-expand-all
          :expand-on-click-node="false"
          :highlight-current="true"
          :check-strictly="true"
          @current-change="hChange"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/detail')">添加员工</el-button>
          <el-button
            size="mini"
            @click="showExcelDialog=true"
          >excel导入</el-button>
          <el-button v-allow="'excel_import'" size="mini" @click="hExport">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <!-- 表格组件 -->
        <el-table :data="list">
          <el-table-column align="center" label="头像">
            <template slot-scope="{ row }">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" :size="30" />
              <span v-else class="username">{{ row.username.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template v-slot="{ row }">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 2">非正式</span>
              <span v-else>无</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template slot-scope="{ row }">
              <el-button size="mini" type="text" @click="$router.push(`/detail/${row.id}`)">查看</el-button>
              <el-button size="mini" type="text" @click="hAssignRole(row.id)">角色</el-button>
              <el-button size="mini" type="text" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination
            background
            layout="prev, pager, next"
            :total="total"
            :current-page="queryParams.page"
            :page-size="queryParams.pagesize"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </div>
    </div>
    <!-- 导出 -->
    <import-excel
      :show-excel-dialog.sync="showExcelDialog"
      @updateExcel="getListData"
    />
    <!-- 角色提示框 -->
    <assign-role
      ref="refAssign"
      :is-assign.sync="isAssign"
      :curr-id="currId"
    />
  </div>
</template>

<script>
import { getTexture } from '@/api/department'
import { arrTree } from '@/utils'
import { delEmployee, getEmployeeList, getExport } from '@/api/employee.js'
import FileSaver from 'file-saver'
import importExcel from './components/import-excel.vue'
import { Message } from 'element-ui'
import AssignRole from './components/assignRole.vue'
export default {
  name: 'Employee',
  components: { importExcel, AssignRole },
  data() {
    return {
      data: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      // 右侧信息列表
      list: [],
      queryParams: {
        departmentId: null,
        page: 1, // 当前页码
        pagesize: 10,
        keyword: '' // 模糊搜索字段
      },
      // 总条数
      total: 0,
      // 导入
      showExcelDialog: false,
      // 角色
      isAssign: false,
      currId: null
    }
  },
  created() {
    this.getListData()
  },
  methods: {
    // 角色
    hAssignRole(id) {
      // 开启弹窗
      this.isAssign = true
      // 获取用户点击的 id 将次 id 传给子组件
      this.currId = id
      // 调用子组件的方法做数据回显
      this.$nextTick(() => {
        this.$refs.refAssign.getEmployee()
      })
    },
    // 删除
    async del(id) {
      // console.log(id)
      // 发送删除请求
      await delEmployee(id)
      // 提醒用户
      Message.success('删除成功')
      // 重新渲染
      this.getListData()
    },
    // 获取数据
    async getListData() {
      const res = await getTexture()
      // 将获取的平铺数据转换为树型结构数据
      this.data = arrTree(res.data, 0)
      // console.log(this.data)
      this.$nextTick(() => {
        // this.$refs.tree.setCheckedKeys([this.depts[0].id])
        // 页面加载显示高亮
        // this.$refs.tree.setCurrentKeys(this.data[0].id)
        this.$refs.tree.setCurrentKey(this.data[0].id)
      })
      // 切换节点时记录id
      this.queryParams.departmentId = this.data[0].id
      // 重新渲染
      this.getList()
    },
    hChange(e) {
      this.queryParams.departmentId = e.id
      this.getList()
    },
    // 获取右侧信息列表
    async getList() {
      const res = await getEmployeeList(this.queryParams)
      // console.log(res)
      // 将获取的数据存放到数组中
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 每页条数
    handleSizeChange(val) {
      // 更新每页显示的条数
      this.queryParams.pagesize = val
      // 将页码变成第一页
      this.queryParams.page = 1
      this.getList()
    },
    // 页码
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.queryParams.page = val
      // 重新渲染
      this.getList()
    },
    // 搜索
    changeValue() {
      // 单位时间内只执行最后一次
      // this的实例上赋值了一个timer的属性
      clearTimeout(this.timer) // 清理上一次的定时器
      this.timer = setTimeout(() => {
        this.queryParams.page = 1
        this.getList()
      }, 300)
    },
    // 导出
    async hExport() {
      const res = await getExport()
      // console.log(res)
      FileSaver.saveAs(res, '员工信息表.xlsx')
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    margin: 10px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
<style>
.el-tree--highlight-current .el-tree-node.is-checked > .el-tree-node__content {
    color: #276FFB;
    background-color: #f0f4ff;
}
</style>

